#include "Arrow.h"

#include "GameGlobal.h"
#include "Archer.h"

using namespace cocos2d;

CArrow *CArrow::CreateArrow(const ArrowInfo &info,  cocos2d::CCNode *pParent, int zOrder)
{
    CArrow *pNewArrow = new CArrow;
    pNewArrow->_InitState(info._fileName, pParent, info._bUseFrame, zOrder);
    pNewArrow->SetParam(info._mass, info._gravity, info._windPower, info._arrowWidth);
    
    return pNewArrow;
    
}

//-----------------------------------------------------------------------------------------------------/
void CArrow::Prepare(cocos2d::CCPoint ptTail, float angle)
{
    m_curAngle = angle;
    float rad = CC_DEGREES_TO_RADIANS(m_curAngle);
    m_ptStartPos.x = ptTail.x + cos(rad) * m_arrowWidth;
    m_ptStartPos.y = ptTail.y + sin(rad) * m_arrowWidth;
    m_pSprArrow->setPosition(ccp(m_ptStartPos.x, m_ptStartPos.y));
    cocos2d::CCPoint ptStartPos = CGameGlobal::RestoreResToLogicPos(ccp(m_ptStartPos.x , m_ptStartPos.y));
    m_ptStartPos = MyPoint(ptStartPos.x, ptStartPos.y);
    m_pSprArrow->setRotation(-m_curAngle);
}

//-----------------------------------------------------------------------------------------------------/
cocos2d::CCPoint CArrow::TailToHead(cocos2d::CCPoint ptTail, float angle)
{
    cocos2d::CCPoint ptRet;
    
    float rad = CC_DEGREES_TO_RADIANS(angle);
    ptRet.x = ptTail.x + cos(rad) * m_arrowWidth;
    ptRet.y = ptTail.y + sin(rad) * m_arrowWidth;
    
    return ptRet;
}

//-----------------------------------------------------------------------------------------------------/
void CArrow::Remove()
{
    if(NULL != m_pSprArrow)
    {
        m_pSprArrow->removeFromParentAndCleanup(true);
        m_pSprArrow = NULL;
    }
}

//-----------------------------------------------------------------------------------------------------/
void CArrow::Shoot(float power, cocos2d::CCPoint ptHead, float angle, list<CArcherBase *> *pTargetList)
{
    CArrowBase::Shoot(power, MyPoint(ptHead.x, ptHead.y), angle, pTargetList);
    
    m_curAngle = angle;
    m_pSprArrow->setPosition(CGameGlobal::GetInstance()->AdjustResPos(ccp(m_ptStartPos.x, m_ptStartPos.y)));
    m_pSprArrow->setRotation(-m_curAngle);
    
    m_pSprArrow->getParent()->reorderChild(m_pSprArrow, m_pSprArrow->getZOrder() - 2);
}

//-----------------------------------------------------------------------------------------------------/
bool CArrow::ProcessTime(float time)
{
    float t2 = time*time;
    float x = (m_startPower* time * cos(m_startRad)) / m_mass - m_curWindPower * t2 *0.5f / m_mass;
    float y = (m_startPower* time * sin(m_startRad)) / m_mass - 0.5*m_gravity * t2;
    
    CCPoint ptCur = ccp(m_ptStartPos.x + x, m_ptStartPos.y + y);
    
    
    float sx = m_startPower * cos(m_startRad) / m_mass - m_curWindPower * time / m_mass;
    float sy = m_startPower * sin(m_startRad)  / m_mass - m_gravity* time;
        
    m_curAngle  = CC_RADIANS_TO_DEGREES(atan2(sy, sx));
    m_pSprArrow->setPosition(CGameGlobal::GetInstance()->AdjustResPos(ptCur));
    m_pSprArrow->setRotation(-m_curAngle);
        
    m_ptCur = MyPoint(ptCur.x, ptCur.y);
    
    return false;
   
}

//-----------------------------------------------------------------------------------------------------/
void CArrow::_InitState(const char *szFileName, cocos2d::CCNode *pParent, bool bUseFrame, int zOrder)
{
    if(bUseFrame)
    {
		m_pSprArrow = CCSprite::spriteWithSpriteFrameName(szFileName);
         
    }
    else
	{
		m_pSprArrow = CCSprite::spriteWithFile(szFileName);
    }
    pParent->addChild(m_pSprArrow, zOrder);
    m_pSprArrow->setAnchorPoint(ccp(1.0, 0.5));
}

const ArrowInfo &CArrowManager::GetArrowInfo(int id)
{
    static ArrowInfo retInfo;
    switch (id) {
        case 1:
            strcpy(retInfo._fileName, "arrow1.png");
            retInfo._bUseFrame = true;
            retInfo._arrowWidth = 130;
            break;
            
        default:
            strcpy(retInfo._fileName, "arrow2.png");
            retInfo._bUseFrame = true;
            retInfo._arrowWidth = 84;

            break;
    }
    
    return retInfo;
}


